home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 August / macformat-027.iso / mac / Shareware City / Developers / PlayerPRO 4.4.1 Dev.Kit / Import⁄Export / 669.2Mad.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-04  |  9.6 KB  |  393 lines  |  [TEXT/MPCC]

  1. /********************                        ***********************/
  2. //
  3. //    Player PRO 4.4x -- 669 to MADx
  4. //
  5. //    Version 1.0    - 12.3.95 ANR
  6. //
  7. //    To use with CodeWarrior 68K or PPC
  8. //
  9. //    Antoine ROSSET
  10. //    16 Tranchees
  11. //    1206 GENEVA
  12. //    SWITZERLAND
  13. //    
  14. //    FAX:            (+41 22) 346 11 97
  15. //    Compuserve:    100277,164
  16. //    Internet:         rosset@dial.eunet.ch
  17. //
  18. /********************                        ***********************/
  19.  
  20.  
  21. #include "669.h"
  22. #include "MAD.h"
  23. #include "RDriver.h"
  24. #include "PPInOut.h"
  25.  
  26. #if defined(powerc) || defined(__powerc)
  27. enum {
  28.         PlayerPROPlug = kCStackBased
  29.         | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
  30.         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof( OSType)))
  31.         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof( Ptr*)))
  32.         | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof( MADPartition*)))
  33.         | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof( PPInfoRec*)))
  34.         | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof( short**)))
  35. };
  36.  
  37. ProcInfoType __procinfo = PlayerPROPlug;
  38. #else
  39. #include <A4Stuff.h>
  40. #endif
  41.  
  42. short Tdecode16( void *msg_buf)
  43. {
  44.   unsigned char *buf = msg_buf;
  45.   
  46.   return ( (short) buf[1] << 8) | ( (short) buf[0]);
  47. }
  48.  
  49. void pStrcpy(register unsigned char *s1, register unsigned char *s2)
  50. {
  51.     register short len, i;
  52.     
  53.     len = *s2;
  54.     for ( i = 0; i <= len; i++) s1[ i] = s2[ i];
  55. }
  56.  
  57. struct Command* GetCommand( register short PosX, register short    TrackIdX, register    struct MusicPattern*    tempMusicPat)
  58. {
  59.     if( PosX < 0) PosX = 0;
  60.     else if( PosX >= tempMusicPat->header.PatternSize) PosX = tempMusicPat->header.PatternSize -1;
  61.         
  62.     return( & (tempMusicPat->Commands[ (tempMusicPat->header.PatternSize * TrackIdX) + PosX]));
  63. }
  64.  
  65. OSErr Convert6692Mad( Ptr    AlienFile, long MODSize, MADPartition    *theMAD)
  66. {
  67.     SixSixNine             *the669 = (SixSixNine*) AlienFile;
  68.     short                 i, PatMax, x, z;
  69.     long                     sndSize, OffSetToSample, OldTicks, temp;
  70.     Ptr                    MaxPtr;
  71.     OSErr                theErr;
  72.     Ptr                    theInstrument[ 64], destPtr;
  73.     unsigned    short        tempS;
  74.     short                Note, Octave;
  75.     char                    *thePasByte;
  76.     
  77.     /**** Variables pour le MAD ****/
  78.     struct Command        *aCmd;
  79.  
  80.     /**** Variables pour le MOD ****/
  81.     
  82.     struct PatSix        *PatInt;
  83.     struct PatCmd        *theCommand;
  84.     struct SampleInfo    *SInfo;
  85.     /********************************/
  86.  
  87.     theMAD->header = (MADSpec*) NewPtrClear( sizeof( MADSpec));    
  88.  
  89.     MaxPtr = (Ptr)((long) the669 + MODSize);
  90.  
  91.     OffSetToSample = 0x1f1L + (long)  the669->NOS * 25L + (long) the669->NOP * 0x600L;
  92.  
  93.     for( i = 0; i < the669->NOS ; i++)
  94.     {
  95.         temp = (long) the669;
  96.         temp += 0x1f1L + i*25L + 13L;
  97.         
  98.         SInfo = (SampleInfo*) temp;
  99.         
  100.         SInfo->length =    Tdecode16( &SInfo->length);
  101.         SInfo->loopStart = Tdecode16( &SInfo->loopStart);
  102.         SInfo->loopEnd = Tdecode16( &SInfo->loopEnd);
  103.                 
  104.         theInstrument[i] = (Ptr) ((long) the669 + (long) OffSetToSample);
  105.         OffSetToSample += SInfo->length;
  106.     }
  107.  
  108.     /******** Le 669 a été lu et analysé ***********/
  109.     /******** Copie des informations dans le MAD ***/
  110.  
  111.     theMAD->header->MADIdentification = 'MADF';
  112.     for(i=0; i<32; i++) theMAD->header->NameSignature[i] = 0;
  113.     for(i=0; i<32; i++) theMAD->header->NameSignature[i] = the669->message[i];
  114.     
  115.     for(i=0; i<64; i++)
  116.     {
  117.         for(x=0; x<32; x++) theMAD->header->fid[i].Filename[x] = 0;
  118.         theMAD->header->fid[i].insSize = 0;
  119.         theMAD->header->fid[i].fineTune = 0;
  120.         theMAD->header->fid[i].volume = 0;
  121.         theMAD->header->fid[i].freq = 1;
  122.         theMAD->header->fid[i].amplitude = 0;
  123.         theMAD->header->fid[i].loopStart = 0;
  124.         theMAD->header->fid[i].loopLenght = 0;
  125.     }
  126.  
  127.     theMAD->header->PatMax = the669->NOP;
  128.     theMAD->header->numPointers = 0;
  129.     for(i=0; i<128; i++) theMAD->header->oPointers[ i] = the669->orderList[ i];
  130.     theMAD->header->Tracks = 8;
  131.     
  132.     for(i=0; i<the669->NOS; i++)
  133.     {
  134.         temp = (long) the669;
  135.         temp += 0x1f1L + i*25L + 13L;
  136.         
  137.         SInfo = (SampleInfo*) temp;
  138.     
  139.     //    for( x = 0; x < 13; x++) theMAD->header->fid[i].Filename[x] = SInfo->InstruFilename[x];
  140.         theMAD->header->fid[i].insSize = SInfo->length;
  141.         theMAD->header->fid[i].fineTune = 0;        //SInfo->fineTune;
  142.         theMAD->header->fid[i].volume = 64;            //SInfo->volume;
  143.         theMAD->header->fid[i].freq = 1;
  144.         theMAD->header->fid[i].amplitude = 8;
  145.         theMAD->header->fid[i].loopStart = 0;    //SInfo->loopStart;
  146.         theMAD->header->fid[i].loopLenght = 0;    //SInfo->loopEnd - SInfo->loopStart;
  147.         
  148.         if( theMAD->header->fid[i].insSize > 0)
  149.         {
  150.             theMAD->instrument[i] = NewPtr( theMAD->header->fid[i].insSize);
  151.             BlockMove( theInstrument[i], theMAD->instrument[i], theMAD->header->fid[i].insSize);
  152.             
  153.             destPtr = theMAD->instrument[i];
  154.             for( temp = 0; temp < theMAD->header->fid[i].insSize; temp++)
  155.             {
  156.                 *(destPtr + temp) -= 0x80;
  157.             }
  158.         }
  159.         else theMAD->instrument[i] = 0L;
  160.     }
  161.  
  162.     for(i  = the669->NOS ; i < 64; i++)
  163.     {
  164.         theMAD->instrument[i] = 0L;
  165.         theMAD->header->fid[i].volume = 64;
  166.         theMAD->header->fid[i].freq = 1;
  167.         theMAD->header->fid[i].amplitude = 8;
  168.     }
  169.     
  170.     PatInt = ( struct PatSix*) the669 + 0x1f1 + the669->NOS * 0x19;
  171.     
  172.     temp = (long) the669;
  173.     temp += 0x1f1L + (long) the669->NOS * 0x19L;
  174.     
  175.     PatInt = ( struct PatSix*) temp;
  176.     
  177.     /***** TEMPORAIRE ********/
  178.     
  179.     theMAD->header->Tracks = 8;
  180. //    theMAD->header->PatMax = 1;
  181.  
  182.     for( i = 0; i < theMAD->header->PatMax; i++)
  183.     {
  184.         theMAD->partition[ i] = (struct    MusicPattern*) NewPtrClear( sizeof( struct MusicPattern) + theMAD->header->Tracks * 64L * sizeof( struct Command));
  185.         theMAD->partition[ i]->header.PatternSize = 64L;
  186.         theMAD->partition[ i]->header.CompressionMode = 'NONE';
  187.         for( x = 0; x < 20; x++) theMAD->partition[ i]->header.PatternName[ x] = 0;
  188.         theMAD->partition[ i]->header.PatBytes = 0L;
  189.         theMAD->partition[ i]->header.unused2 = 0L;
  190.     
  191.         for( x = 0 ; x < 64; x++)
  192.         {
  193.             for( z = 0; z<theMAD->header->Tracks; z++)
  194.             {
  195.                 aCmd = GetCommand( x, z, theMAD->partition[ i]);
  196.                 
  197.                 theCommand = &PatInt[ i].Cmds[ x][ z];
  198.                 thePasByte = ( Ptr) theCommand;
  199.                 if( (Ptr) theCommand >= MaxPtr) Debugger();
  200.                 
  201.                 if( thePasByte[0] == 0xFF)
  202.                 {
  203.                     aCmd->EffectCmd = 0;
  204.                     aCmd->EffectArg = 0;
  205.                     aCmd->InstrumentNo = 0;
  206.                     aCmd->AmigaPeriod = 0;
  207.                 }
  208.                 else if( thePasByte[0] == 0xFE)
  209.                 {
  210.                     aCmd->InstrumentNo = 0;
  211.                     aCmd->AmigaPeriod = 0;
  212.                     aCmd->EffectCmd = 0;        // 0x0C;
  213.                     aCmd->EffectArg = 0;        // theCommand->Volume;
  214.                 }
  215.                 else
  216.                 {
  217.                     aCmd->InstrumentNo = theCommand->Instru + 1;
  218.                 
  219.             /*        if( theCommand->AmigaPeriod > 0 && theCommand->AmigaPeriod < 60)
  220.                         aCmd->AmigaPeriod = theCommand->AmigaPeriod;
  221.                     else theCommand->AmigaPeriod = 0;*/
  222.  
  223.                     aCmd->AmigaPeriod = theCommand->AmigaPeriod;
  224.                     
  225.                     Note = (aCmd->AmigaPeriod & 0xF0) >> 4;
  226.                 //    Note -= 2;
  227.                     Octave = (aCmd->AmigaPeriod & 0x0F);
  228.                     
  229.                     aCmd->AmigaPeriod = Note*12 + Octave;
  230.                 }
  231.                 
  232.                 if( thePasByte[2] == 0xFF)
  233.                 {
  234.                     aCmd->EffectCmd = 0;
  235.                     aCmd->EffectArg = 0;
  236.                 }
  237.                 else
  238.                 {
  239.                     aCmd->EffectCmd = 0;
  240.                     aCmd->EffectArg = 0;
  241.                 }
  242.             }
  243.         }
  244.     }
  245.     
  246.     return noErr;
  247. }
  248.  
  249. OSErr Extract669Info( PPInfoRec *info, Ptr AlienFile)
  250. {
  251.     SixSixNine    *the669 = (SixSixNine*) AlienFile;
  252.     long            PatternSize;
  253.     short        i;
  254.     short        maxInstru;
  255.     short        tracksNo;
  256.     
  257.     /*** Signature ***/
  258.     
  259.     info->signature = '669 ';
  260.     
  261.     /*** Internal name ***/
  262.     
  263.     the669->message[ 30] = '\0';
  264.     pStrcpy( info->internalFileName, CtoPstr( the669->message));
  265.  
  266.     /*** Total Patterns ***/
  267.     
  268.     info->totalPatterns = 0;
  269.     
  270.     /*** Partition Length ***/
  271.     
  272.     info->partitionLength = 0;
  273.     
  274.     /*** Total Instruments ***/
  275.     
  276.     info->totalInstruments = 0;
  277.     
  278.     pStrcpy( info->formatDescription, "\p669 Plug");
  279.  
  280.     return noErr;
  281. }
  282.  
  283. OSErr Test669File( Ptr AlienFile)
  284. {
  285. SixSixNine    *the669 = (SixSixNine*) AlienFile;
  286.  
  287. if( the669->marker == 0x6669 || the669->marker == 0x6966) return   noErr;
  288. else return  fileNotSupportedByThisPlug;
  289. }
  290.  
  291. OSErr main( OSType order, FSSpec *AlienFileFSSpec, MADPartition *MadFile, PPInfoRec *info, short *MADpitchTable)
  292. {
  293.     OSErr    myErr;
  294.     Ptr        AlienFile;
  295.     short    vRefNum, iFileRefI;
  296.     long    dirID, sndSize;
  297.     
  298. #ifndef powerc
  299.     long    oldA4 = SetCurrentA4();             //this call is necessary for strings in 68k code resources
  300. #endif
  301.  
  302.     HGetVol( 0L, &vRefNum, &dirID);
  303.     HSetVol( 0L, AlienFileFSSpec->vRefNum, AlienFileFSSpec->parID);
  304.  
  305.     myErr = noErr;
  306.  
  307.     switch( order)
  308.     {
  309.         case 'IMPL':
  310.             myErr = FSOpen( AlienFileFSSpec->name, 0, &iFileRefI);
  311.             if( myErr == noErr)
  312.             {
  313.                 GetEOF( iFileRefI, &sndSize);
  314.             
  315.                 // ** MEMORY Test Start
  316.                 AlienFile = NewPtr( sndSize * 2L);
  317.                 if( AlienFile == 0L) myErr = needMoreMemory;
  318.                 // ** MEMORY Test End
  319.                 
  320.                 else
  321.                 {
  322.                     DisposPtr( AlienFile);
  323.                     
  324.                     AlienFile = NewPtr( sndSize);
  325.                     myErr = FSRead( iFileRefI, &sndSize, AlienFile);
  326.                     if( myErr == noErr)
  327.                     {
  328.                         myErr = Test669File( AlienFile);
  329.                         if( myErr == noErr)
  330.                         {
  331.                             myErr = Convert6692Mad( AlienFile,  GetPtrSize( AlienFile), MadFile);
  332.                         }
  333.                     }
  334.                     DisposPtr( AlienFile);    AlienFile = 0L;
  335.                 }
  336.                 FSClose( iFileRefI);
  337.             }
  338.         break;
  339.         
  340.         case 'TEST':
  341.             myErr = FSOpen( AlienFileFSSpec->name, 0, &iFileRefI);
  342.             if( myErr == noErr)
  343.             {
  344.                 sndSize = 1024L;
  345.                 
  346.                 AlienFile = NewPtr( sndSize);
  347.                 if( AlienFile == 0L) myErr = needMoreMemory;
  348.                 else
  349.                 {
  350.                     myErr = FSRead( iFileRefI, &sndSize, AlienFile);
  351.                     myErr = Test669File( AlienFile);
  352.                     
  353.                     DisposPtr( AlienFile);    AlienFile = 0L;
  354.                 }
  355.                 FSClose( iFileRefI);
  356.             }
  357.         break;
  358.  
  359.         case 'INFO':
  360.             myErr = FSOpen( AlienFileFSSpec->name, 0, &iFileRefI);
  361.             if( myErr == noErr)
  362.             {
  363.                 GetEOF( iFileRefI, &info->fileSize);
  364.             
  365.                 sndSize = 5000L;    // Read only 5000 first bytes for optimisation
  366.                 
  367.                 AlienFile = NewPtr( sndSize);
  368.                 if( AlienFile == 0L) myErr = needMoreMemory;
  369.                 else
  370.                 {
  371.                     myErr = FSRead( iFileRefI, &sndSize, AlienFile);
  372.                     if( myErr == noErr)
  373.                     {
  374.                         myErr = Extract669Info( info, AlienFile);
  375.                     }
  376.                     DisposPtr( AlienFile);    AlienFile = 0L;
  377.                 }
  378.                 FSClose( iFileRefI);
  379.             }
  380.         break;
  381.         
  382.         default:
  383.             myErr = orderNotImplemented;
  384.         break;
  385.     }
  386.  
  387.     HSetVol( 0L, vRefNum, dirID);
  388.  
  389.     #ifndef powerc
  390.         SetA4( oldA4);
  391.     #endif
  392.     return myErr;
  393. }